import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-09-20
 * Time: 11:06
 */
public class JDBC {
    public static void JDBCDemo() throws Exception{
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        //构建连接参数
        String URL ="jdbc:mysql://127.0.0.1:3306/学生选课系统";
        String USER = "root";
        String PASSWORD = "root";

        //获取数据库连接
        Connection conn = DriverManager.getConnection(URL,USER,PASSWORD);

        //编写sql语句
        String sql = "select * from 教师";
        String sql1 = "update 教师 set 密码 = 654321";

        //获取执行sql对象
        Statement stat = conn.createStatement();

        //执行sql语句
        ResultSet set = stat.executeQuery(sql);
        int count = stat.executeUpdate(sql1);

        //处理结果
        if(count != 0){
            System.out.println("执行成功");
        }else{
            System.out.println("执行失败");
        }
        System.out.println();
        System.out.println("*******************************教师信息如下：*********************************");
        int i = 1;
        while(set.next()){
            int accountNum = set.getInt(1);
            int accountNum1 = set.getInt("账号");
            String name = set.getString(2);
            String gender = set.getString(3);
            int age = set.getInt(4);
            String rank = set.getString(5);
            long phone = set.getLong(6);
            System.out.println("教师 " + i++ + " {" +
                    "教师账号： " + accountNum +
                    "  姓名：" + name +
                    "  性别：" + gender +
                    "  年龄：" + age +
                    "  职称：" + rank +
                    "  联系方式：" + phone +
                    '}');
            i++;
        }
        System.out.println("*****************************************************************************");
        System.out.println();

        //关闭资源
        set.close();
        stat.close();
        conn.close();
    }
    public static void main(String[] args) throws Exception{
        JDBCDemo();
    }
}